<?php

/**
 * @author aisyah
 * @copyright 2012
 */

//include "user.php";


class User_Dao{
    
    var $db;
    var $order = array();
    var $user = array();
    
    public function __construct(Database $db){
        $this->db = $db;
    }
    
    //mendapatkan informasi order--dalam bentuk daftar
    public function get_order($id_customer, $posisi=null, $batas=null){
        $this->order = "";
        
        //$id_customer = $user->customer_id;
        if(!is_null($posisi) and !is_null($batas)){
            $q = "SELECT * FROM corder WHERE customer=".$id_customer." LIMIT $posisi,$batas";
        }else{
            $q = "SELECT * FROM corder WHERE customer=".$id_customer;
        }
        
        //print_r($q);
        $order = $this->db->select_table($q);
        //print_r($order."-".$q);
        if(count($order)>0){
            foreach($order as $hasil){
                $r_order = new Order;
                
                $r_order->nomor_po = $hasil['nomor_po'];
                $r_order->order_date = $hasil['order_date'];
                $r_order->nilai = $hasil['total'];
                $r_order->status = $hasil['status'];
                
                $this->order[] = $r_order;
            
            }
        }else{
            return false;
        }
        
        
        return $this->order;
    }
    
    //mendapatkan informasi user
    public function get_user_information($user_id){
        //$this->db->connect->connect();
        
        //$this->class_db->hasil="";
        
        $q = "SELECT user.user_id as user_id,
                    user.username as username,
                    user.password as password,
                    user.reset_quest as reset_quest,
                    user.reset_answer as reset_answer,
                    
                    customer.customer_id as customer_id,
                    customer.name as name,
                    customer.street as street,
                    customer.kecamatan as kecamatan,
                    customer.city as city,
                    customer.province as province,
                    customer.zip_code as zip_code,
                    customer.telephone as telephone,
                    customer.photo as photo
        
        
         FROM user INNER JOIN customer ON user.user_id = customer.user_id
                    WHERE user.user_id=".$user_id;
                    
                    //print_r($q);
         
         $result = $this->db->select_table($q);
         //print_r($result);
         //$result = mysql_query($q);
         //var_dump($result);
         if($result>0){
            foreach($result as $data){
            //while($data=mysql_fetch_array($result)){
                $user = new User();
                
                $user->user_id = $data['user_id'];
                $user->username = $data['username'];
                $user->password = $data['password'];
                $user->reset_quest = $data['reset_quest'];
                $user->reset_answer = $data['reset_answer'];
                
                //data
                $user->customer_id = $data['customer_id'];
                $user->name = $data['name'];
                $user->street = $data['street'];
                $user->kecamatan = $data['kecamatan'];
                $user->city = $data['city'];
                $user->province = $data['province'];
                $user->zip_code = $data['zip_code'];
                $user->telephone = $data['telephone'];
                $user->photo = $data['photo'];
                //print_r($user->username);
                $this->user[] = $user;
            }
            
            //$this->db->connect->disconnect();
            
            return $this->user;
            
         }else{
            return false;
         }
        
        
    }
    
    //merubah informasi user di database
    public function get_user($user_id){
        $q = "SELECT *      
        
         FROM user WHERE user_id=".$user_id;                    
                   
         $result = $this->db->select_table($q);
         
         if($result>0){
            foreach($result as $data){
            //while($data=mysql_fetch_array($result)){
                $user = new User();
                
                $user->user_id = $data['user_id'];
                $user->username = $data['username'];
                $user->password = $data['password'];
                $user->reset_quest = $data['reset_quest'];
                $user->reset_answer = $data['reset_answer'];              
                
                //print_r($user->username);
                $this->user[] = $user;
            }
            
            //$this->db->connect->disconnect();
            
            return $this->user;
            
         }else{
            return false;
         }
    }  
    
}


class User {
    
    var $user_id;
    var $username;
    var $password;
    var $reset_quest;
    var $reset_answer;
    
    //data
    var $customer_id;
    var $name;
    var $street;
    var $kecamatan;
    var $city;
    var $province;
    var $zip_code;
    var $telephone;
    var $photo;
    
}

class Order{
    var $nomor_po;
    var $order_date;
    var $nilai;
    var $status;
}



?>